package org.example.sensortest;

import java.util.Collection;
import java.util.List;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.widget.ArrayAdapter;

import org.example.sensortest.SensorAdapter.Item;

public class SensorAdapter extends ArrayAdapter<Item>
{
	public interface Item
	{
		int getType();
	}
	
	private class ItemHeader implements Item
	{
		private int type;
		
		private ItemHeader(int type)
		{
			this.type = type;
		}
		
		@SuppressWarnings("deprecation")
		private String getLabel()
		{
			switch(type)
			{
				case Sensor.TYPE_ACCELEROMETER:
					return "ACCELEROMETER";
				case Sensor.TYPE_AMBIENT_TEMPERATURE:
					return "	AMBIENT_TEMPERATURE";
				case Sensor.TYPE_GRAVITY:
					return "GRAVITY";
				case Sensor.TYPE_GYROSCOPE:
					return "GYROSCOPE";
				case Sensor.TYPE_LIGHT:
					return "LIGHT";
				case Sensor.TYPE_LINEAR_ACCELERATION:
					return "LINEAR_ACCELERATION";
				case Sensor.TYPE_MAGNETIC_FIELD:
					return "MAGNETIC_FIELD";
				case Sensor.TYPE_ORIENTATION:
					return "ORIENTATION";
				case Sensor.TYPE_PRESSURE:
					return "PRESSURE";
				case Sensor.TYPE_PROXIMITY:
					return "PROXIMITY";
				case Sensor.TYPE_RELATIVE_HUMIDITY:
					return "RELATIVE_HUMIDITY";
				case Sensor.TYPE_ROTATION_VECTOR:
					return "ROTATION_VECTOR";
				case Sensor.TYPE_TEMPERATURE:
					return "TEMPERATURE";
				default:
					return "Unbekannt";
			}
		}
		
		public int getType()
		{
			return type;
		}
		
		@Override
		public String toString()
		{
			return String.format("\n%s: %s", this.type, this.getLabel());
		}
	}
	
	public class ItemSensor implements Item
	{
		public Sensor getSensor()
		{
			return sensor;
		}

		private Sensor sensor;
		
		private ItemSensor(Sensor sensor)
		{
			this.sensor = sensor;
		}
		
		@Override
		public String toString()
		{
			return String.format("%s/%s v%s", sensor.getVendor(), sensor.getName(), sensor.getVersion());
		}
		
		@Override
		public int getType()
		{
			return sensor.getType();
		}
	}
	
	private SensorManager sensorManager;
	
	public SensorAdapter(Context context)
	{
		super(context, android.R.layout.simple_spinner_item);
		
		super.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
		
		List<Sensor> list = sensorManager.getSensorList(Sensor.TYPE_ALL);
		//List<Sensor> list = sensorManager.getSensorList(Sensor.TYPE_ROTATION_VECTOR);
		
		int index;
		for(Sensor sensor : list)
		{
			index = getIndexForType(sensor.getType());
			if(index == -1)
			{
				super.add(new ItemHeader(sensor.getType()));
				super.add(new ItemSensor(sensor));
			}
			else
				super.insert(new ItemSensor(sensor), index + 1);
		}
	}
	
	@Override
	public void add(Item object)
	{
		throw new UnsupportedOperationException();
	}
	
	@Override
	public void addAll(Collection<? extends Item> collection)
	{
		throw new UnsupportedOperationException();
	}
	
	@Override
	public void addAll(Item... items)
	{
		throw new UnsupportedOperationException();
	}
	
	@Override
	public boolean areAllItemsEnabled()
	{
		return false;
	}
	
	private int getIndexForType(int type)
	{
		for(int i = 0; i < this.getCount(); i++)
		{
			Item item = this.getItem(i);
			if(item instanceof ItemSensor)
				continue;
			if(item.getType() == type)
				return i;
		}
		return -1;
	}
	
	@Override
	public int getViewTypeCount()
	{
		return 1;
	}
	
	@Override
	public void insert(Item object, int index)
	{
		throw new UnsupportedOperationException();
	}
	
	@Override
	public boolean isEnabled(int position)
	{
		Item item = getItem(position);
		if(item instanceof ItemSensor)
			return true;
		return false;
	}
}
